package com.all.play.myapplication.controller.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.all.play.myapplication.R;
import com.all.play.myapplication.moudle.Model;
import com.all.play.myapplication.moudle.bean.UserInfo;
import com.hyphenate.chat.EMClient;

/**
 * Created by 聪明一只哈 on 2018/3/30.
 */

public class SplashActivity extends AppCompatActivity {

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(isFinishing()){//当前页面已经退出
                return;
            }

            //判断进入主页面,还是登录界面
            toMainLogin();
        }
    };

    /**
     *判断进入主页面,还是登录界面
     *@author 聪明一只哈
     *@time 2018/3/30 14:35
     */
    private void toMainLogin() {

        Model.getInstance().getGloableThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                //判断账号是否登陆过
                if(EMClient.getInstance().isLoggedInBefore()){

                    //先获取当前登录用户的信息(存储)
                    UserInfo accountByHxId = Model.getInstance().getUserAccountDao().getAccountByHxId(EMClient.getInstance().getCurrentUser());
                    if(accountByHxId==null){
                        startActivity(new Intent(SplashActivity.this,LoginActivity.class));
                    }else {
                        //进入主页面
                        startActivity(new Intent(SplashActivity.this,MainActivity.class));
                    }

                }else {
                    //没登陆过,进入注册页面
                    startActivity(new Intent(SplashActivity.this,LoginActivity.class));
                }
                finish();
            }
        });

    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        handler.sendMessageDelayed(Message.obtain(),2000);


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}
